package org.stvd.service.oauth.impl;

import java.util.Base64;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.oauth.OauthClientDetails;
import org.stvd.repository.oauth.OauthClientDetailsDao;
import org.stvd.service.base.impl.BaseServiceImpl;
import org.stvd.service.oauth.OauthClientDetailsService;

/**
 * Oauth客户端Service实现类
 * 
 * @author houzx
 */
@Service("oauthClientDetailsService")
public class OauthClientDetailsServiceImpl extends BaseServiceImpl<OauthClientDetails>
    implements OauthClientDetailsService {

    @Resource(name = "OauthClientDetailsDao")
    private OauthClientDetailsDao oauthClientDetailsDao;

    @Override
    public QueryResult<OauthClientDetails> queryOauthClientResult(int start, int limit, String clientId,
        String clientName) {
        return oauthClientDetailsDao.queryOauthClientResult(start, limit, clientId, clientName);
    }

    @Override
    public boolean hasOauthRedirectUri(String clientId, String redirectUri) {
        List<OauthClientDetails> clientList = oauthClientDetailsDao.listOauthClientByClientId(clientId);
        boolean hasRedirectUri = false;
        if(clientList != null && clientList.size() > 0) {
            for (OauthClientDetails oauthClientDetails : clientList) {
                String webServerRedirectUriStr = oauthClientDetails.getWebServerRedirectUri();
                if(!StringUtil.isEmpty(webServerRedirectUriStr)) {
                    for (String webServiceRedirectUri : webServerRedirectUriStr.split(",")) {
                        if(redirectUri.equals(webServiceRedirectUri)) {
                            hasRedirectUri = true;
                            break;
                        }
                    }
                }
            }
        }
        return hasRedirectUri;
    }
    
    @Override
    public boolean hasOauthClientId(String clientId) {
        List<OauthClientDetails> clientList = oauthClientDetailsDao.listOauthClientByClientId(clientId);
        if (clientList != null && clientList.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    @CacheEvict(value = "clientDetailsCache", key = "#clientDetails.getClientId()")
    public void UpdateclientSecret(String clientId) {
        OauthClientDetails oauthClientDetails = this.findByPk(OauthClientDetails.class, clientId);
        if (oauthClientDetails != null) {
            oauthClientDetails.setClientSecret(generateSecret());
        }
        this.update(oauthClientDetails);
    }

    
    @Override
    @Transactional
    @CacheEvict(value = "serviceCache", key = "#pk")
    public void delete(Class<OauthClientDetails> t, Object pk) {
         super.delete(t, pk);
    }

    @Override
    @Transactional
    @CacheEvict(value = "serviceCache", key = "#entity.getClientId()")
    public void insert(OauthClientDetails entity) {
         super.insert(entity);
    }

    @Override
    @Transactional
    @CacheEvict(value = "serviceCache", key = "#entity.getClientId()")
    public void update(OauthClientDetails entity) {
         super.update(entity);
    }

    @Override
    public String generateSecret() {
        String uuid = StringUtil.generateShortUuid() + StringUtil.generateShortUuid() + StringUtil.generateShortUuid();
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(uuid.getBytes());
    }

}
